from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import Http404
from detlefschneider.gallery.models import Category, Gallery

def category(request, category, view='list'):
    try:
        category_obj = Category.objects.get(slug=category)
    except Category.DoesNotExist:
        raise Http404    

    galleries = Gallery.objects.filter(categories=category_obj)

    template = 'list.html'
    nav_active = category_obj.slug
    if view == 'grid':
        template = 'grid.html'
        nav_active = category_obj.slug + '_all'

    return render_to_response(
                'gallery/templates/' + template, 
                {
                    'galleries': galleries,
                    'nav_active': nav_active,
                    'page_title': _get_page_title(category_obj.name),
                    'category': category_obj,
                }, 
                context_instance=RequestContext(request)
    )

def _get_page_title(category_name):
    if category_name == 'Latest':
        return 'Portfolio'

    return category_name

def gallery(request, gallery):
    template = 'gallery.html'
    if request.is_ajax():
        template = 'gallery_ajax.html'

    try:
        gallery_obj = Gallery.objects.get(slug=gallery)
    except Gallery.DoesNotExist:
        raise Http404

    return render_to_response(
                'gallery/templates/' + template, 
                {
                    'gallery': gallery_obj,
                }, 
                context_instance=RequestContext(request)
    )